﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Acms.Exceptions;

public partial class Templates_SchoolDetails_Default : BaseTemplatePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnEdit.Click += new EventHandler(btnEdit_Click);
        btnEditB.Click += new EventHandler(btnEditB_Click);
        btnCancel.Click += new EventHandler(btnCancel_Click);
        btnChangePassword.Click += new EventHandler(btnChangePassword_Click);
        btnCancelPassword.Click += new EventHandler(btnCancelPassword_Click);
        btnSavePassword.Click += new EventHandler(btnSavePassword_Click);
        btnSave.Click += new EventHandler(btnSave_Click);
        if (!IsPostBack)
        {
            AcmsContext.EnsureLoggedIn(this);
            DataBind();
        }
    }

    

    void btnSave_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            //check school not already in db
            List<User> tmp = Acms.Bll.User.GetAllByCustomProperty(AcmsContext.SiteId, "School Name", txtSchoolName.Text.Trim());
            if (tmp != null && tmp.Count > 0 && tmp[0].Id != User.Identity.User.Id)
            {
                pnlErrors.Visible = true;
                ulErrors.InnerHtml = string.Format("<li>{0}</li>", "This school has already been registered");
                return;
            }
            Acms.Bll.User user = Acms.Bll.User.GetById(User.Identity.User.Id);
            user.Address1 = txtAddress1.Text.Trim();
            user.Address2 = txtAddress2.Text.Trim();
            user.EmailAddress = txtEmailAddress.Text.Trim();
            user.FirstName = txtContactFirstName.Text.Trim();
            user.LastName = txtContactLastName.Text.Trim();
            user.MobileTel = txtContactTel.Text.Trim();
            user.PostCode = txtPostCode.Text.Trim();
            user.Town = txtTown.Text.Trim();
            user.County = txtCounty.Text.Trim();
            user.UserName = txtUsername.Text.Trim();
            user.SetCustomProperty("School Name", txtSchoolName.Text.Trim());
            user.SetCustomProperty("School Type", ddlType.SelectedValue);
            user.SetCustomProperty("School Tel", txtSchoolTel.Text.Trim());
            user.SetCustomProperty("Secondary Contact Name", txtSecondaryContact.Text.Trim());
            user.SetCustomProperty("Pledge Text", txtPledgeText.Text.Trim());

            try
            {
                user.Update();
            }
            catch (DuplicateEmailException)
            {
                pnlErrors.Visible = true;
                ulErrors.InnerHtml = string.Format("<li>{0}</li>", "This email address has already been registered");
                return;
            }
            catch (DuplicateUsernameException)
            {
                pnlErrors.Visible = true;
                ulErrors.InnerHtml = string.Format("<li>{0}</li>", "This username has already been registered");
                return;
            }

            User.Identity.RefreshUser();
            pnlReadOnly.Visible = true;
            pnlChangePassword.Visible = pnlEdit.Visible = false;
            DataBind();

        }
    }

    void btnSavePassword_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            Acms.Bll.User user = Acms.Bll.User.GetById(User.Identity.User.Id);
            if (user.SetPassword(txtCurrentPassword.Text.Trim(), txtPassword.Text.Trim()))
            {
                user.Update();
                pnlChangePassword.Visible = pnlEdit.Visible = false;
                pnlReadOnly.Visible = true;
            }
            else
            {

            }
        }
    }

    void btnCancelPassword_Click(object sender, EventArgs e)
    {
        pnlChangePassword.Visible = pnlEdit.Visible = false;
        pnlReadOnly.Visible = true;
    }

    void btnChangePassword_Click(object sender, EventArgs e)
    {
        pnlChangePassword.Visible = true; 
        pnlEdit.Visible = false;
        pnlReadOnly.Visible = false;
    }

    void btnCancel_Click(object sender, EventArgs e)
    {
        pnlEdit.Visible = false;
        pnlReadOnly.Visible = true;
        DataBind();
    }

    void btnEdit_Click(object sender, EventArgs e)
    {
        pnlEdit.Visible = true;
           pnlReadOnly.Visible = false;
    }
    void btnEditB_Click(object sender, EventArgs e)
    {
        pnlEdit.Visible = true;
        pnlReadOnly.Visible = false;
    }

}
